﻿using System;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class PrintContractViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        ILoanExecutionService _LoanExecutionService;
        /// <summary>
        /// Initializes a new instance of the <see cref="PrintContractViewModel"/> class.
        /// </summary>
        /// <param name="loanRepository">The loan repository.</param>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="loanExService">The loan ex service.</param>
        public PrintContractViewModel(ILoanRepository loanRepository, IAdministrationRepository administrationRepository, ILoanExecutionService loanExService)
        {
            _LoanRepository = loanRepository;
            _AdministrationRepository = administrationRepository;
            _LoanExecutionService = loanExService;
        }

        /// <summary>
        /// Gets the loan execution by ID.
        /// </summary>
        /// <param name="loanExecutionID">The loan execution ID.</param>
        /// <returns></returns>
        public ILoanExecution GetLoanExecutionByID(Guid loanExecutionID)
        {
            return _LoanRepository.GetLoanExecution(loanExecutionID);
        }

        /// <summary>
        /// Gets the current committee.
        /// </summary>
        /// <returns></returns>
        public ICommittee GetCurrentCommittee()
        {
            return _AdministrationRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
        }

        /// <summary>
        /// Generates the execution.
        /// </summary>
        /// <param name="loanID">The loan ID.</param>
        /// <param name="executionDate">The execution date.</param>
        /// <param name="checkDate">The check date.</param>
        /// <param name="guarantorName">Name of the guarantor.</param>
        /// <param name="guarantorSSN">The guarantor SSN.</param>
        /// <returns></returns>
        public ILoanExecution GenerateExecution(Guid loanID, DateTime executionDate, DateTime checkDate, string guarantorName, string guarantorSSN)
        {
            ICustomerLoan loan = _LoanRepository.GetCustomerLoanByID(loanID);
            IBoardLoan boardLoan = _LoanRepository.FindBoardLoans(b => b.LoanID == loanID).Last();
            ILoanExecution loanExecution = _LoanExecutionService.CreateLoanExecution(loan, boardLoan, executionDate, "***che", checkDate, guarantorName, guarantorSSN, "** notes", false);
            _LoanRepository.CreateLoanExecution(loanExecution);
            return loanExecution;
        }


        /// <summary>
        /// Gets the loan object.
        /// </summary>
        /// <param name="loanID">The loan ID.</param>
        /// <returns></returns>
        public ICustomerLoan GetLoanObject(Guid loanID)
        {
            return _LoanRepository.GetCustomerLoanByID(loanID);
        }
    }
}
